package com.xin.di.uav.operate.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xin.di.uav.common.request.order.CancelWaybillRequest;
import com.xin.di.uav.common.request.order.CarrierWaybillRequest;
import com.xin.di.uav.common.request.order.PlaceOrderRequest;
import com.xin.di.uav.common.request.order.SelectOrderRequest;
import com.xin.di.uav.operate.service.IntraCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author LTQ
 */
@Service
public class IntraCityServiceImpl implements IntraCityService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${test-pre-url:http://ie-uocs-int-apis.int.sfcloud.local:8000/}")
    private String testPreUrl;

    @Override
    public JSONObject carrierWaybill(CarrierWaybillRequest carrierWaybillRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(carrierWaybillRequest));
        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(testPreUrl + "/uocs-tes/fyWaybill/carrierWaybill", entity, JSONObject.class);
        JSONObject result = response.getBody();
//        JSONObject jsonObject = JSONObject.parseObject("{\n" +
//                "    \"requestId\": null,\n" +
//                "    \"success\": true,\n" +
//                "    \"errorCode\": null,\n" +
//                "    \"date\": \"2020-11-17 18:08:31\",\n" +
//                "    \"timestamp\": 1605607711793,\n" +
//                "    \"version\": \"1.0\",\n" +
//                "    \"errorMessage\": null,\n" +
//                "    \"obj\": null\n" +
//                "}");
        return result;
    }

    @Override
    public JSONObject placeOrder(PlaceOrderRequest placeOrderRequest) {
        placeOrderRequest.setGpsType(1);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(placeOrderRequest));
        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(testPreUrl + "/uocs-tes/fyWaybill/placeOrder", entity, JSONObject.class);
        JSONObject result = response.getBody();
//        JSONObject jsonObject = JSONObject.parseObject("{\n" +
//                "    \"requestId\": null,\n" +
//                "    \"success\": true,\n" +
//                "    \"errorCode\": null,\n" +
//                "    \"date\": \"2020-11-17 18:08:31\",\n" +
//                "    \"timestamp\": 1605607711793,\n" +
//                "    \"version\": \"1.0\",\n" +
//                "    \"errorMessage\": null,\n" +
//                "    \"obj\": null\n" +
//                "}");
        return result;
    }

    @Override
    public JSONObject selectTerminalList() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(testPreUrl + "/uocs-tes/fyWaybill/selectTerminalList", entity, JSONObject.class);
        JSONObject result = response.getBody();
//        JSONObject jsonObject = JSONObject.parseObject("{\n" +
//                "    \"requestId\": null,\n" +
//                "    \"success\": true,\n" +
//                "    \"errorCode\": null,\n" +
//                "    \"date\": \"2020-11-17 18:08:31\",\n" +
//                "    \"timestamp\": 1605607711793,\n" +
//                "    \"version\": \"1.0\",\n" +
//                "    \"errorMessage\": null,\n" +
//                "    \"obj\": null\n" +
//                "}");
        return result;
    }

    @Override
    public JSONObject selectOrder(SelectOrderRequest selectOrderRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tcWaybillNo", selectOrderRequest.getTcWaybillNo());
        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(testPreUrl + "/uocs-tes/fyWaybill/selectOrder", entity, JSONObject.class);
        JSONObject result = response.getBody();
//        JSONObject jsonObject = JSONObject.parseObject("{\n" +
//                "    \"requestId\": null,\n" +
//                "    \"success\": true,\n" +
//                "    \"errorCode\": null,\n" +
//                "    \"date\": \"2020-11-17 18:08:31\",\n" +
//                "    \"timestamp\": 1605607711793,\n" +
//                "    \"version\": \"1.0\",\n" +
//                "    \"errorMessage\": null,\n" +
//                "    \"obj\": null\n" +
//                "}");
        return result;
    }

    @Override
    public JSONObject cancelWaybill(CancelWaybillRequest cancelWaybillRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("tcWaybillNo", cancelWaybillRequest.getTcWaybillNo());
        jsonObject.put("cancelReason", cancelWaybillRequest.getCancelReason());
        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(testPreUrl + "/uocs-tes/fyWaybill/cancelWaybill", entity, JSONObject.class);
        JSONObject result = response.getBody();
//        JSONObject jsonObject = JSONObject.parseObject("{\n" +
//                "    \"requestId\": null,\n" +
//                "    \"success\": true,\n" +
//                "    \"errorCode\": null,\n" +
//                "    \"date\": \"2020-11-17 18:08:31\",\n" +
//                "    \"timestamp\": 1605607711793,\n" +
//                "    \"version\": \"1.0\",\n" +
//                "    \"errorMessage\": null,\n" +
//                "    \"obj\": null\n" +
//                "}");
        return result;
    }
}
